<?php 
// 给PHP设置内存占用，这里为1个G也就是1024M
ini_set("memory_limit", "1024M");

require dirname(__FILE__)."/../core/init.php";

function saveData($url,$i){
    $html = requests::get($url);

    $user_name_selector = '@<td>用户名</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>性别</td>@';
    $user_name = selector::select($html, $user_name_selector, 'regex');

    $user_sex_selector = '@<td>性别</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>个人签名</td>@';
    $user_sex = selector::select($html, $user_sex_selector, 'regex');

    $user_sign_selector = '@<td>个人签名</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>空间访问量</td>@';
    $user_sign = selector::select($html, $user_sign_selector, 'regex');

    $user_view_selector = '@<td>空间访问量</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>邮箱验证状态</td>@';
    $user_view = selector::select($html, $user_view_selector, 'regex');

    $user_email_status_selector = '@<td>邮箱验证状态</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*</table>[^xyz]*</div>[^xyz]*<div class="pbm mbm bbda cl space_profile_box">[^xyz]*<h2 class="mbn">[^xyz]*论坛统计@';
    $user_email_status = selector::select($html, $user_email_status_selector, 'regex');

    $user_friends_selector = '@<td>好友数</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>相册数</td>@';
    $user_friends = selector::select($html, $user_friends_selector, 'regex');

    $user_albums_selector = '@<td>相册数</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>回帖数</td>@';
    $user_albums = selector::select($html, $user_albums_selector, 'regex');

    $user_replies_selector = '@<td>回帖数</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>主题数</td>@';
    $user_replies = selector::select($html, $user_replies_selector, 'regex');

    $user_publishes_selector = '@<td>主题数</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*</table>[^xyz]*</div>[^xyz]*<div class="pbm mbm bbda cl space_profile_box">[^xyz]*<h2 class="mbn">[^xyz]*活跃概况@';
    $user_publishes = selector::select($html, $user_publishes_selector, 'regex');

    $user_group_selector = '@<td>用户组</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>在线时间</td>@';
    $user_group = selector::select($html, $user_group_selector, 'regex');

    $user_online_time_selector = '@<td>在线时间</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>注册时间</td>@';
    $user_online_time = selector::select($html, $user_online_time_selector, 'regex');

    $user_reg_time_selector = '@<td>注册时间</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>上次活动时间</td>@';
    $user_reg_time = selector::select($html, $user_reg_time_selector, 'regex');

    $user_lastest_login_selector = '@<td>上次活动时间</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>上次发表时间</td>@';
    $user_lastest_login = selector::select($html, $user_lastest_login_selector, 'regex');

    $user_lastest_pub_selector = '@<td>上次发表时间</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>所在时区</td>@';
    $user_lastest_pub = selector::select($html, $user_lastest_pub_selector, 'regex');

    $user_timezone_selector = '@<td>所在时区</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*</table>[^xyz]*</div>[^xyz]*<div class="pbm mbm bbda cl space_profile_box">[^xyz]*<h2 class="mbn">[^xyz]*统计信息@';
    $user_timezone = selector::select($html, $user_timezone_selector, 'regex');

    $user_use_space_selector = '@<td>已用空间</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>威望</td>@';
    $user_use_space = selector::select($html, $user_use_space_selector, 'regex');

    $user_prestige_selector = '@<td>威望</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>煤球</td>@';
    $user_prestige = selector::select($html, $user_prestige_selector, 'regex');

    $user_coalball_selector = '@<td>煤球</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>魅力</td>@';
    $user_coalball = selector::select($html, $user_coalball_selector, 'regex');

    $user_charm_selector = '@<td>魅力</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*</table>[^xyz]*</div>[^xyz]*</div>@';
    $user_charm = selector::select($html, $user_charm_selector, 'regex');

    if(empty($user_name)){
        echo 'error-'.$i."\r\n";
        // $data = fopen("data.log", "a+");
        // fwrite($data,$i."未找到\r\n");
        // fclose($data);
    }else{
        if($user_sex!=='男' && $user_sex!=='女' && $user_sex!=='未知'){
            $user_sex_selector = '@<td>性别</td>[^xyz]*<td>(.*)</td>[^xyz]*</tr>[^xyz]*<tr class="column2">[^xyz]*<td>微博地址</td>@';
            $user_sex = selector::select($html, $user_sex_selector, 'regex');
        }

        if($user_sex=='男'){
            $user_sex=1;
        }elseif($user_sex=='女'){
            $user_sex=2;
        }else{
            $user_sex=0;
        }

        if(empty($user_sign)){
            $user_sign='无';
        }

        if($user_email_status=='已验证'){
            $user_email_status=1;
        }else{
            $user_email_status=0;
        }

        if(empty($user_reg_time)){
            $user_reg_time=0;
        }else{
            $user_reg_time=strtotime($user_reg_time);
        }

        if(empty($user_lastest_login)){
            $user_lastest_login=0;
        }else{
            $user_lastest_login=strtotime($user_lastest_login);
        }

        if(empty($user_lastest_pub)){
            $user_lastest_pub=0;
        }else{
            $user_lastest_pub=strtotime($user_lastest_pub);
        }

        if(empty($user_online_time)){
            $user_online_time=0;
        }else{
            $user_online_time=substr($user_online_time, 0, strpos($user_online_time,' 小时'));
        }

        if(empty($user_group)){
            $user_group='无';
        }else{
            $result=str_replace('&nbsp;', '', $user_group);  
            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $result)>0){
                $user_group=$result;
            }else{
                if(strpos($result,'扩展用户组')===false){
                    $la=strpos($result,'>');
                    $ra=strpos($result,'</');
                    $resul=substr($result, $la+1, $ra);
                    $ra=strpos($resul,'</');
                    $resul=substr($resul, 0, $ra);
                    $user_group=$resul;
                }else{
                    $resu=selector::select($result, '@<font color="#999999">(.*)</font><li>@', 'regex');
                    $res=selector::select($result, '@</em><font color="#999999">(.*)</font></li>@', 'regex');
                    $user_group=$resu.'('.$res.')';
                }
            }
        }
        
        $data=array(
                'user_id'=>$i,
                'user_name'=>$user_name,
                'user_sex'=>$user_sex,
                'user_sign'=>$user_sign,
                'user_view'=>(int)$user_view,
                'user_email_status'=>(int)$user_email_status,
                'user_albums'=>(int)$user_albums,
                'user_replies'=>(int)$user_replies,
                'user_publishes'=>(int)$user_publishes,
                'user_friends'=>(int)$user_friends,
                'user_group'=>$user_group,
                'user_online_time'=>(int)$user_online_time,
                'user_reg_time'=>(int)$user_reg_time,
                'user_lastest_login'=>(int)$user_lastest_login,
                'user_lastest_pub'=>(int)$user_lastest_pub,
                'user_timezone'=>$user_timezone,
                'user_use_space'=>$user_use_space,
                'user_prestige'=>(int)$user_prestige,
                'user_prestige'=>(int)$user_prestige,
                'user_coalball'=>(int)$user_coalball,
                'user_charm'=>(int)$user_charm,
                'created_at'=>time(),
            );
        $rows = db::insert('meizu_users', $data);
        //echo 'success-'.$i."\r\n";
    }

    
}


for($i=1;$i<1000000000;$i++){
    $url = 'http://bbs.meizu.cn/home.php?mod=space&uid='.$i.'&do=profile&from=space';
    saveData($url,$i);
}
